package com.hnkywl.sts.action.organ;

import com.hnkywl.sts.entity.organ.Gsjg;
import com.hnkywl.sts.entity.organ.Lj;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.entity.sys.Gncz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.organ.LjwhKclsService;
import com.hnkywl.sts.service.sys.GnczService;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-4
 * Time: 上午12:01
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/organ/ljwh_kcls")
public class LjwhKclsAction extends SimpleActionSupport {
    private LjwhKclsService ljwhKclsService;
    private YgService ygService;
    //路径实例
    private Lj lj;
    //路径ID
    private Long ljId;
    //下游路径ID
    private String xyIds;
    //路径类型——库存零售
    private Integer lx = 4;
    //经营机构名称
    private String jyjg;
    //经营机构Id
    private Long jyjgId;
    //员工
    private Yg yg;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows = 10;

    //远程ajax验证的值
    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;


    //功能操作
    private List<Gncz> gnczList;
    private GnczService gnczService;

    @Action("ljwh_kcls")
    public String ljwh_kcls() {
        yg =YgService.getCurrentYg();
        gnczList = gnczService.findGnczByJsAndUrl(request.getServletPath());
        return SUCCESS;
    }

    //库存零售路径的查询分页
    @Action("ljwh_kclsList")
    public void ljwh_kclsList() {
        this.renderJson(this.ljwhKclsService.findAllLjwhKcls(jyjg,jyjgId,page,rows));
    }

    @Action("ljwh_kcls_content")
    public String ljkclscontent() {
        if (ljId != null) {
            lj = this.ljwhKclsService.find(ljId);
            xyIds = contactId(lj.getXy());
        }
        return SUCCESS;
    }

    //路径明细
    @Action("ljwh_kcls_detail")
    public String ljwhKclsDetail() {
        lj = this.ljwhKclsService.find(ljId);
        return SUCCESS;
    }

    @Action("ljwh_kcls_info")
    public void ljwhKclsInfo() {
        this.renderJson(this.ljwhKclsService.findLjwhKcls(jyjgId));
    }

    @Action("ljwh_kcls_infoxy")
    public void ljwhKclsInfoxy() {
        lj = this.ljwhKclsService.find(ljId);
        Map<String, Object> otherParam = new HashMap<String, Object>();
        otherParam.put("lx", "xy");
        String ljjson = BaseUtils.fetchCollectionToJson(lj.getXy(), otherParam, "id", "qc","jc");
        this.renderJson("[" + ljjson + "]");
    }

    private String contactId(List<Gsjg> xylist) {
        String result = "";
        for (Gsjg xy : xylist) {
            result += xy.getId() + ",";
        }
        return StringUtils.isNotEmpty(result) ? result.substring(0, result.length() - 1) : "";
    }

    //添加库存零售路径
    @Action("add_lj_kcls")
    public void addLjKcls() {
        try {
            this.ljwhKclsService.addLjKcls(lj);
            renderText("{\"statusCode\":\"200\",\"message\":\"库存零售路径添加成功!\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //修改库存零售路径
    @Action("update_lj_kcls")
    public void updateLjKcls() {
        try {
            this.ljwhKclsService.updateLjKcls(lj);
            renderText("{\"statusCode\":\"200\",\"message\":\"库存零售路径修改成功!\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //删除库存零售路径
    @Action("ljwh_kcls_commit")
    public void commitLjKcls() {
        try {
            this.ljwhKclsService.commitLjKcls(ljId);
            renderText("{\"statusCode\":\"200\",\"message\":\"库存零售路径提交成功!\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //删除库存零售路径
    @Action("ljwh_kcls_delete")
    public void deleteLjKcls() {
        try {
            this.ljwhKclsService.deleteLjKcls(ljId);
            renderText("{\"statusCode\":\"200\",\"message\":\"库存零售路径删除成功!\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //解冻库存零售路径
    @Action("start_lj")
    public void startLj() {
        try {
            this.ljwhKclsService.startLj(ljId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"库存零售路径解冻成功！\", \"operationType\":\"update\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    //冻结库存零售路径
    @Action("stop_lj")
    public void stopLj() {
        try {
            this.ljwhKclsService.stopLj(ljId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"库存零售路径冻结成功！\", \"operationType\":\"update\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("ajax_contrary_validate")
    public void contraryValidate() {
        if (ljwhKclsService.ajaxValidate(jyjgId))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    //=============getter and setter============

    public Lj getLj() {
        return lj;
    }

    public void setLj(Lj lj) {
        this.lj = lj;
    }

    public Long getLjId() {
        return ljId;
    }

    public void setLjId(Long ljId) {
        this.ljId = ljId;
    }

    public String getXyIds() {
        return xyIds;
    }

    public void setXyIds(String xyIds) {
        this.xyIds = xyIds;
    }

    public Integer getLx() {
        return lx;
    }

    public void setLx(Integer lx) {
        this.lx = lx;
    }

    public String getJyjg() {
        return jyjg;
    }

    public void setJyjg(String jyjg) {
        this.jyjg = jyjg;
    }

    public Long getJyjgId() {
        return jyjgId;
    }

    public void setJyjgId(Long jyjgId) {
        this.jyjgId = jyjgId;
    }

    public Yg getYg() {
        return yg;
    }

    public void setYg(Yg yg) {
        this.yg = yg;
    }

    public int getPage() {
        return page;
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public List<Gncz> getGnczList() {
        return gnczList;
    }

    public void setGnczList(List<Gncz> gnczList) {
        this.gnczList = gnczList;
    }

    @Autowired
    public void setGnczService(GnczService gnczService) {
        this.gnczService = gnczService;
    }

    @Autowired
    public void setLjwhKclsService(LjwhKclsService ljwhKclsService) {
        this.ljwhKclsService = ljwhKclsService;
    }

    @Autowired
    public void setYgService(YgService ygService) {
        this.ygService = ygService;
    }
}
